<?php

namespace app\admin\controller;

use think\Controller;

/**
 * Class GoodsList
 * @package app\admin\controller
 * 货品列表管理控制器
 */
class Goodslist extends Controller
{
    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\GoodsList();
    }

    //首页
    public function index()
    {
        $gid = input('param.gid');
        //分配数据
        $data = db('goods-list')->select();
//        $ress=[];
        foreach ($data as $k => $v) {
            $data[$k]['combine'] = explode(',', $v['combine']);
        }
        foreach($data as $k=>$v){
            foreach($v['combine'] as $kk=>$vv){
                $data[$k]['combine'][$kk] = db('goods-attribute')
                                        ->where('gid',$gid)
                                        ->where('gaid',$vv)
                                        ->field('gtvalue')
                                        ->find();
            }

        }
        //寻找类型属性表
        $gaData = db('goods-attribute')
            ->alias('ga')
            ->join('__TYPE-ATTRIBUTE__ ta', 'ga.taid = ta.taid')
            ->where('ga.gid', $gid)
            ->where('class', 1)
            ->field('ta.taid,ta.taname')
            ->select();
        $this->assign('gaData', $gaData);
        p($gaData);
        //视图
        return view('', compact('data'));
    }

    //添加
    public function add()
    {
        $gid = input('param.gid');
        //1通过商品id来获取对应的类型属性
        $specData = db('type-attribute')
            ->alias('ta')
            ->join('__GOODS__ g', 'ta.tid = g.tid')
            ->where('g.gid', $gid)
            ->where('class', 1)
            ->field('ta.taid,ta.taname')
            ->select();
        //2.通过taid获取对应商品的选中规格
        foreach ($specData as $k => $v) {
            $specData[$k]['select'] = db('goods-attribute')
                ->where('taid', $v['taid'])
                ->where('gid', $gid)
                ->field(['gaid,gtvalue'])
                ->select();
        }
        $this->assign('specData', $specData);
        //3,获取商品旧数据
        $oldGoodsData = db('goods')->where('gid', $gid)->field('stock,gnumber')->find();
        $this->assign('oldGoodsData', $oldGoodsData);
        //添加
        if (request()->isPost()) {
            $data = input('post.');
            $res = $this->db->add($data);
            if ($res['valid']) {
                $this->success($res['msg'], url('index', ['gid' => $gid]));
            } else {
                $this->error($res['msg']);
            }
        }
        //视图
        return view();
    }
    //编辑
    public function edit(){
        $glid=input('param.glid');
        $gid=input('param.gid');
        $oldData = db('goods-list')->where('glid', $glid)->find();
        $oldData['combine'] = explode(',', $oldData['combine']);
        foreach($oldData['combine'] as $k=>$v){
            $oldData['combine'][$k] = db('goods-attribute')
                ->where('gid',$gid)
                ->where('gaid',$v)
                ->field('gtvalue')
                ->find();
        }
        p($oldData);
        //1通过商品id来获取对应的类型属性
        $specData = db('type-attribute')
            ->alias('ta')
            ->join('__GOODS__ g', 'ta.tid = g.tid')
            ->where('g.gid', $gid)
            ->where('class', 1)
            ->field('ta.taid,ta.taname')
            ->select();
        $this->assign('specData', $specData);
        //编辑
        if (request()->isPost()) {
            $data = input('post.');
            $res = $this->db->edid($data);
            if ($res['valid']) {
                $this->success($res['msg'], url('index',['gid' => $gid]));
            } else {
                $this->error($res['msg']);
            }
        }
        //视图
        return view('', compact('oldData'));
    }
    //删除
    public function del($glid){
        $res=$this->db->del($glid);
        if ($res['valid']) {
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }
}

